﻿using Godot;
using System;
using System.Collections.Generic;
public class ImageSet
{

    public Vector2 size { set; get; } = new Vector2(16, 16);
    public Vector2 coord { set; get; }
    public string Directory = "res://Texture/Item";
    public string name { set; get; }
    public string group { set; get; }
}
public class ItemMateria : SWMaterial
{
    public ImageSet imageset;
    public bool Hide;
    public delegate ItemData CreateItemDataAction();
    public delegate bool onPlayerUseItem(PlayerUseItemEvent e);
    public Texture texture;
    public int MaxAmount = 64;
    public int Fuel;
    public bool isblock = false;
    public List<String> Lore = new List<String>();
    public CreateItemDataAction GetItemData;
    public onPlayerUseItem PlayerUseItem;
    public ItemMateria(string name, ImageSet imageSet = null) : base(name, MaterialType.Item)
    {
        this.imageset = imageSet;
        if (imageset != null && imageset.coord != null)
        {
            String _name = name;
            if (imageset.name != null && imageset.name != "") _name = imageset.name;
            if (imageset.group != null || imageset.group != "")
                texture = (Texture)GD.Load($"{imageset.Directory}/{imageset.group}/{_name}.png");
            else
                texture = (Texture)GD.Load($"{imageset.Directory}/{_name}.png");
            AtlasTexture at = new AtlasTexture
            {
                Atlas = texture,
                Region = new Rect2(imageset.coord * 16, new Vector2(imageSet.size.x, imageSet.size.y))
            };
            texture = at;
        }
        else
        {
            texture = (Texture)GD.Load($"res://Texture/Item/{name}.png");
            if (texture == null)
            {
                GD.PrintErr($"[缺失物品材质] {name} !");
                texture = (Texture)GD.Load($"res://Texture/Block/{name}.png");
            }

        }
    }
    public ItemMateria SetImageSet(ImageSet imageSet)
    {
        this.imageset = imageSet;
        return this;
    }
    //设置在创造栏中隐藏
    public ItemMateria SetHide(bool h)
    {
        Hide = h;
        return this;
    }
    public ItemMateria SetPlayerUseItem(onPlayerUseItem e)
    {
        this.PlayerUseItem = e;
        return this;
    }
    public ItemMateria SetItemData(CreateItemDataAction cida)
    {
        this.GetItemData = cida;
        return this;
    }
    public ItemMateria SetFuel(int fuel)
    {
        Fuel = fuel;
        return this;
    }
    public ItemMateria SetTextrue(String dir)
    {
        texture = (Texture)GD.Load($"res://Texture/{dir}.png");
        return this;
    }
    public ItemMateria AddLore(String lore)
    {
        Lore.Add(lore);
        return this;
    }
    public ItemMateria SetDisplayName(string DisPlayName)
    {
        this.DisPlayName = DisPlayName;
        return this;
    }
    public ItemMateria SetMaxAmount(int Amount)
    {
        this.MaxAmount = Amount;
        return this;
    }
    public ItemMateria SetBlock(bool b)
    {
        this.isblock = b;
        return this;
    }
    public ItemStack GetItemStack(int amount = 1)
    {
        ItemStack item = new ItemStack();
        item.DispalyName = DisPlayName;
        item.Name = Name;
        item.Amount = amount;

        //GD.Print($"Test : {item.toJson()}");
        return item;
    }
    public bool HasLore()
    {
        return Lore.Count != 0;
    }
    public String GetLore()
    {
        String str = "";
        for (int i = 0, max = Lore.Count; i < max; i++)
        {

            if (i != max - 1)
            {
                str += Lore[i] + "\n";
            }
            else
            {
                str += Lore[i];
            }
        }
        if (Fuel > 0)
        {
            String nextline = "";
            if (Lore.Count != 0)
                nextline = "\n";
            str += $"{nextline}燃烧:{(int)(Fuel / 20)}秒";
        }

        return str;
    }
}
